package com.microtf.inscription.web.controller.admin;


import com.microtf.inscription.framework.form.Form;
import com.microtf.inscription.framework.form.FormGenerateService;
import com.microtf.inscription.framework.pojo.vo.MiniAppVo;
import com.microtf.inscription.framework.service.MiniAppService;
import com.microtf.inscription.framework.vo.AjaxDto;
import com.microtf.inscription.jpa.entity.MiniApp;
import com.microtf.inscription.web.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;

import javax.validation.ConstraintViolationException;
import java.lang.reflect.Method;
import java.util.Optional;

/**
 * 小程序类功能上传文件管理
 * @author guliuzhong
 */
@Controller
@RequestMapping("/admin/miniApp")
@Slf4j
public class MiniAppController extends BaseController {
    FormGenerateService formGenerateService;
//    WxUploadService wxUploadService;

    MiniAppService miniAppService;
//    WeChatResourceService wechatResourceService;

    @Autowired
    public void setFormGenerateService(FormGenerateService formGenerateService) {
        this.formGenerateService = formGenerateService;
    }

//    @Autowired
//    public void setWxUploadService(WxUploadService wxUploadService) {
//        this.wxUploadService = wxUploadService;
//    }

    @Autowired
    public void setMiniAppService(MiniAppService miniAppService) {
        this.miniAppService = miniAppService;
    }

//    @Autowired
//    public void setWechatResourceService(WeChatResourceService wechatResourceService) {
//        this.wechatResourceService = wechatResourceService;
//    }

    @RequestMapping(value = "/list/{page}", method = {RequestMethod.GET})
    public ModelAndView list(ModelAndView modelAndView, @PathVariable(value = "page",required = false) Integer page) {
        Page<MiniApp> list = miniAppService.getList(page, null, 10);
        modelAndView.addObject("items", list.toList());
        modelAndView.addObject("pageIndex", list.getPageable().getPageNumber() + 1);
        modelAndView.addObject("totalPages", list.getTotalPages());
        modelAndView.addObject("total", list.getTotalElements());
        modelAndView.setViewName("admin/miniapp/list");
        modelAndView.addObject("site", getSiteSetting());
        return modelAndView;
    }
    @RequestMapping(value = "/edit/{appId}", method = {RequestMethod.GET})
    public ModelAndView app(ModelAndView modelAndView, @PathVariable(value = "appId",required = false) Integer appId) {
        Optional<MiniApp> miniAppOptional = miniAppService.getMiniApp(appId);
        MiniApp miniApp = miniAppOptional.orElse(new MiniApp());
        MiniAppVo miniAppVo = miniAppService.toVo(miniApp);
        Form formBySettingName = formGenerateService.generalFormData(miniAppVo);
        modelAndView.addObject("form1", formBySettingName);
        modelAndView.addObject("site", getSiteSetting());
        modelAndView.setViewName("admin/miniapp/app");
        return modelAndView;
    }
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxDto save(MiniAppVo miniAppVo, ModelAndView modelAndView)  throws ConstraintViolationException {
        this.validatePojoAndThrow(miniAppVo);
        MiniApp miniApp1;
        if(miniAppVo.getId()!=null){
            Optional<MiniApp> miniApp = miniAppService.getMiniApp(miniAppVo.getId());
            miniApp1 = miniApp.orElse(new MiniApp());
        }else{
            miniApp1=new MiniApp();
        }

        MiniApp miniApp = miniAppService.toJpa(miniAppVo, miniApp1);
        MiniApp save = miniAppService.save(miniApp);
        AjaxDto ajaxDto=new AjaxDto();
        ajaxDto.setAutoJump(0);
        Method loginPage;
        try {
            loginPage = MiniAppController.class.getMethod("list", ModelAndView.class,Integer.class);
            ajaxDto.setLink(MvcUriComponentsBuilder.fromMethod(MiniAppController.class, loginPage, modelAndView, 1).build().toUriString());
        } catch (NoSuchMethodException e) {
            log.error(e.getMessage());
        }
        ajaxDto.setMessage("设置成功"+save.getAppId());
        return ajaxDto;
    }
//    @RequestMapping(value = "/wx-list-news/{appId}/{page}", method = {RequestMethod.GET})
//    @ResponseBody
//    public ModelAndView wxListNews(@PathVariable(name = "appId") Integer appId,@PathVariable(name = "page",required = false) Integer page,ModelAndView modelAndView) {
//        try{
//            wechatResourceService.init(appId,null);
//            page=page<=0?1:page;
//            PageRequest pageRequest=PageRequest.of(page-1,4);
//            Page<NewMaterial> newMaterialPage = wechatResourceService.listNews(pageRequest);
//            modelAndView.addObject("items", newMaterialPage.toList());
//            modelAndView.addObject("pageIndex", newMaterialPage.getPageable().getPageNumber()+1);
//            modelAndView.addObject("totalPages", newMaterialPage.getTotalPages());
//            modelAndView.addObject("total", newMaterialPage.getTotalElements());
//            modelAndView.addObject("site", getSiteSetting());
//            modelAndView.addObject("appId", appId);
//        } finally {
//            wechatResourceService.release();
//        }
//        modelAndView.setViewName("admin/miniapp/wxlistnews");
//        return modelAndView;
//    }
//    @RequestMapping(value = "/wx-delete-news/{appId}/{mediaId}", method = {RequestMethod.GET})
//    @ResponseBody
//    public AjaxDto wxListNews(@PathVariable(name = "appId") Integer appId,@PathVariable(name = "mediaId") String mediaId) {
//        try{
//            wechatResourceService.init(appId,null);
//            Boolean aBoolean = wechatResourceService.deleteNews(mediaId);
//            AjaxDto ajaxDto=new AjaxDto();
//            ajaxDto.setAutoJump(1);
//            if(aBoolean){
//                ajaxDto.setMessage("删除成功");
//            }else{
//                ajaxDto.setMessage("删除失败");
//            }
//            return ajaxDto;
//        } finally {
//            wechatResourceService.release();
//        }
//    }
}
